package com.yhc.my.an_meishia.activities;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.yhc.my.an_meishia.R;
import com.yhc.my.an_meishia.bean.Users;

import cn.bmob.sms.BmobSMS;
import cn.bmob.sms.exception.BmobException;
import cn.bmob.sms.listener.RequestSMSCodeListener;
import cn.bmob.sms.listener.VerifySMSCodeListener;
import cn.bmob.v3.listener.SaveListener;

public class LoginActivity extends Activity implements View.OnClickListener {

    private EditText etPhone;
    private EditText etPass;
    private EditText etCode;
    private Button btGet;
    private Users users;
    private Button btZc;


    //注册界面
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        //初始化
       BmobSMS.initialize(this, "4a05211f8d45c01ceb4ba271298a3a5b");
    }

    private void initView() {
        etPhone = (EditText) findViewById(R.id.et_phone);
        etPass = (EditText) findViewById(R.id.et_password);
        etCode = (EditText) findViewById(R.id.code);
        btGet = (Button) findViewById(R.id.bt_get);
        btZc = (Button) findViewById(R.id.bt_zc);
        btGet.setOnClickListener(this);
        btZc.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        users = new Users();
        final String phone = etPhone.getText().toString();
        final String mima = etPass.getText().toString();
        String code = etCode.getText().toString();
        switch (v.getId()) {
            case R.id.bt_get:
                if (phone.length() != 11) {
                    Toast.makeText(this, "请输入11位有效手机号码", Toast.LENGTH_SHORT).show();
                } else {
                  BmobSMS.requestSMSCode(LoginActivity.this, phone, "yhc", new RequestSMSCodeListener() {
                        @Override
                        public void done(Integer integer, BmobException e) {
                            if (e == null) {
                                Toast.makeText(LoginActivity.this, "验证码发送成功，请尽快使用", Toast.LENGTH_SHORT).show();
                            } else {
                                Log.i("TAG", "----------------- "+phone);
                                Toast.makeText(LoginActivity.this, "验证码发送失败，请检查网络连接"+e.getErrorCode(), Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                }
                break;
            case R.id.bt_zc:
                if (phone.length() == 0 || mima.length() == 0 || phone.length() != 11) {
                    Toast.makeText(this, "手机号或验证码输入不合法", Toast.LENGTH_SHORT).show();
                } else {
                    BmobSMS.verifySmsCode(this, phone, code, new VerifySMSCodeListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e == null) {
                                users.setPhoneNumber(phone);
                                users.setPassword(mima);
                                users.save(new SaveListener<String>() {
                                    @Override
                                    public void done(String s, cn.bmob.v3.exception.BmobException e) {
                                        if (e == null){

                                        }
                                    }
                                });
                                Toast.makeText(LoginActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(LoginActivity.this, "注册失败"+e.getErrorCode(), Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
                break;

        }
    }
}
